package my.edu.model.bills.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import my.edu.model.bills.purchase.entity.PurchaseReturnOrder;
import my.edu.model.bills.purchase.entity.PurchaseReturnOrderDetail;
import my.edu.model.bills.purchase.entity.dto.PurchaseOrderSearchDTO;
import my.edu.model.bills.purchase.entity.dto.PurchaseReturnDTO;
import my.edu.model.bills.purchase.entity.dto.PurchaseReturnDetailOrderDTO;
import my.edu.model.bills.purchase.mapper.PurchaseReturnOrderDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;



@Service
public class PurchaseReturnOrderDetailServiceImpl extends ServiceImpl<PurchaseReturnOrderDetailMapper, PurchaseReturnOrderDetail> implements IPurchaseReturnOrderDetailService{

    @Override
    @Transactional
    public void saveBatchByOrderDTO(PurchaseReturnDTO purchaseReturnDTO, Long noId) {
        List<PurchaseReturnDetailOrderDTO> purchaseReturnDetailDTOList = purchaseReturnDTO.getPurchaseReturnDetailDTOList();
        Long purchaseOrderId = purchaseReturnDTO.getPurchaseOrderId();
        String purchaseOrderNo = purchaseReturnDTO.getPurchaseOrderNo();
        List<PurchaseReturnOrderDetail> list = purchaseReturnDetailDTOList.stream().map(i -> {
            PurchaseReturnOrderDetail detail = BeanUtil.copyProperties(i, PurchaseReturnOrderDetail.class);
            detail.setPurchaseReturnOrderId(noId);
            return detail;
        }).toList();
        saveBatch(list);
    }

    @Override
    @Transactional
    public void updateBatchByOrderDTO(PurchaseReturnDTO purchaseReturnDTO, Long id) {
        List<PurchaseReturnDetailOrderDTO> purchaseReturnDetailDTOList = purchaseReturnDTO.getPurchaseReturnDetailDTOList();
        Long purchaseOrderId = purchaseReturnDTO.getPurchaseOrderId();
        String purchaseOrderNo = purchaseReturnDTO.getPurchaseOrderNo();
        remove(Wrappers.<PurchaseReturnOrderDetail>lambdaQuery().eq(PurchaseReturnOrderDetail::getPurchaseReturnOrderId,id));
        List<PurchaseReturnOrderDetail> purchaseReturnOrderDetails = purchaseReturnDetailDTOList.stream().map(i -> {
            PurchaseReturnOrderDetail detail = BeanUtil.copyProperties(i, PurchaseReturnOrderDetail.class);
            detail.setPurchaseReturnOrderId(id);
            detail.setPurchaseOrderId(purchaseOrderId);
            detail.setPurchaseOrderNo(purchaseOrderNo);
            return detail;
        }).toList();
        saveBatch(purchaseReturnOrderDetails);
    }

    @Override
    public void removeByOrderId(List<Long> ids) {
        remove(Wrappers.<PurchaseReturnOrderDetail>lambdaQuery()
                .in(PurchaseReturnOrderDetail::getPurchaseReturnOrderId,ids)
        );
    }

    @Override
    public List<PurchaseReturnOrderDetail> getReportList(PurchaseOrderSearchDTO purchaseOrderSearchDTO) {
        MPJLambdaWrapper<PurchaseReturnOrderDetail> wrapper = new MPJLambdaWrapper<PurchaseReturnOrderDetail>()
                .selectAll(PurchaseReturnOrderDetail.class)
                .leftJoin(PurchaseReturnOrder.class, PurchaseReturnOrder::getId, PurchaseReturnOrderDetail::getPurchaseReturnOrderId)
                .between(purchaseOrderSearchDTO.getBeginTime() != null,
                        PurchaseReturnOrder::getOrderDate, purchaseOrderSearchDTO.getBeginTime(), purchaseOrderSearchDTO.getEndTime())
                .eq(purchaseOrderSearchDTO.getSupplierId()!= null,
                        PurchaseReturnOrder::getSupplierId, purchaseOrderSearchDTO.getSupplierId())
                .and(StringUtils.hasText(purchaseOrderSearchDTO.getMaterialParam()), i->i
                        .like(PurchaseReturnOrderDetail::getProductNo, purchaseOrderSearchDTO.getMaterialParam())
                        .or()
                        .like(PurchaseReturnOrderDetail::getProductName, purchaseOrderSearchDTO.getMaterialParam()));
        List<PurchaseReturnOrderDetail> purchaseOrderDetailPage = baseMapper.selectJoinList(wrapper);
        return purchaseOrderDetailPage;
    }
}